package com.tjhk.java.tech.thread.completable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @https://blog.csdn.net/sermonlizhi/article/details/123356877
 *
 * https://blog.csdn.net/finalheart/article/details/87615546?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-87615546-blog-123356877.235%5Ev28%5Epc_relevant_t0_download&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-87615546-blog-123356877.235%5Ev28%5Epc_relevant_t0_download&utm_relevant_index=2
 */
public class CompletableFutureTest {
    public static void main(String[] args) {
        CompletableFuture<Integer> cs = CompletableFuture.supplyAsync(()->{
            int res = 0;
            for (int i = 0; i < 100; i++) {
                res++;
            }
            System.out.println("第一次运算："+res);
            return res;
        }).thenApplyAsync(n->{
            int res = n*n;
            System.out.println("第二次运算："+res);
            return res;
        });
        try {
            System.out.println(cs.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
